package com.myway.utils.http;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.*;
import java.net.URLEncoder;

public class HttpsUtils_p12 {

    private static final String requestUrl = "https://ecommerce.ufc.ge:18443/ecomm2/MerchantHandler";

    public static String httpsRequest(TBCParams tbcParams) throws Exception {
        String result = "";
        HttpPost httpPost = new HttpPost(requestUrl);
        try{
            CloseableHttpClient sslc = new SSLClient();
            StringEntity entity = new StringEntity(params(tbcParams));
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/x-www-form-urlencoded");
            httpPost.setEntity(entity);
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
                @Override
                public String handleResponse(final HttpResponse response)
                        throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        return entity != null ? EntityUtils.toString(entity) : null;
                    } else {
                        throw new ClientProtocolException("Exception:response status: " + status);
                    }
                }
            };
            result = sslc.execute(httpPost, responseHandler);
        }catch(Exception e){
            result = e.getMessage();
            throw new Exception(e.getMessage());
        }
        return result;
    }

    private static String params(TBCParams tbcParams) throws Exception {
        StringBuffer result = new StringBuffer();
        if(tbcParams != null){
            if(StringUtils.isNoneBlank(tbcParams.getCommand()))
                result.append("command="+tbcParams.getCommand());
            if(StringUtils.isNoneBlank(tbcParams.getAmount()))
                result.append("&amount="+tbcParams.getAmount());
            if(StringUtils.isNoneBlank(tbcParams.getCurrency()))
                result.append("&currency="+tbcParams.getCurrency());
            if(StringUtils.isNoneBlank(tbcParams.getTrans_id())){
                String transId = URLEncoder.encode(tbcParams.getTrans_id(),"UTF-8");
                result.append("&trans_id="+transId);
            }
            if(StringUtils.isNoneBlank(tbcParams.getClient_ip_addr()))
                result.append("&client_ip_addr="+tbcParams.getClient_ip_addr());
            if(StringUtils.isNoneBlank(tbcParams.getLanguage()))
                result.append("&language="+tbcParams.getLanguage());
            if(StringUtils.isNoneBlank(tbcParams.getDescription()))
                result.append("&description="+tbcParams.getDescription());
            if(StringUtils.isNoneBlank(tbcParams.getMsg_type()))
                result.append("&msg_type="+tbcParams.getMsg_type());
        }
        return result.toString();
    }

    public static String getTransId(String amount,String currency) throws Exception {
        TBCParams tbcParams = new TBCParams();
        tbcParams.setCommand("v");
        tbcParams.setLanguage("EN");
        tbcParams.setDescription("UFCTEST");
        tbcParams.setMsg_type("SMS");
        if(StringUtils.isNoneBlank(amount))
            tbcParams.setAmount(amount);
        if(StringUtils.isNoneBlank(currency))
            tbcParams.setCurrency(currency);
        return httpsRequest(tbcParams);
    }

    public static String getTransStatus(String transId) throws Exception{
        TBCParams tbcParams = new TBCParams();
        tbcParams.setCommand("c");
        if(StringUtils.isNoneBlank(transId))
            tbcParams.setTrans_id(transId);
        return httpsRequest(tbcParams);
    }

    public static String refund(String transId) throws Exception {
        TBCParams tbcParams = new TBCParams();
        tbcParams.setCommand("k");
        if(StringUtils.isNoneBlank(transId))
            tbcParams.setTrans_id(transId);
        return httpsRequest(tbcParams);
    }

    public static String reversal(String transId,String amount) throws Exception {
        TBCParams tbcParams = new TBCParams();
        tbcParams.setCommand("r");
        if(StringUtils.isNoneBlank(transId))
            tbcParams.setTrans_id(transId);
        if(StringUtils.isNoneBlank(amount))
            tbcParams.setAmount(amount);
        return httpsRequest(tbcParams);
    }
}
